<?php

namespace Medicina\BackendBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Medicina\BackendBundle\Form\PersonaType;
use Symfony\Component\HttpFoundation\Response;

class InscriptoController extends Controller {

    public function editAction($id) {

        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('MedicinaKernelBundle:Persona')->find($id);
        $editForm = $this->createForm(new PersonaType(), $entity);
        return $this->render('BackendBundle:Persona:form.html.twig', array(
                    'entity' => $entity,
                    'form' => $editForm->createView(),
                    'type' => 'inscripto'                            
                ));
    }

    public function updateAction($id) {
        return $this->editEntity('MedicinaKernelBundle:Persona', $id);
    }

    private function editEntity($entity, $id) {
        $em = $this->getDoctrine()->getEntityManager();
        $entity = $em->getRepository($entity)->find($id);
        $return = array();
        if (!$entity) {
            $this->createMsgEdit($return,false);
        } else {
            $editForm = $this->createForm(new PersonaType(), $entity);
            $request = $this->getRequest();
            $editForm->bindRequest($request);
            
            if ($editForm->isValid()) {
                try{
                $em->persist($entity);
                $em->flush();
                $this->createMsgEdit($return, true, $entity);
                  } catch (\PDOException $e) {
                    $this->createMsgEdit($return, false, $entity,array(array("key"=>"#PersonaType_dni", "msj"=>"El dni esta en uso")));
                      
                  }
            }  else {
                print_r($editForm->getErrors());
                print_r($editForm->get('dni')->getErrors());
                print_r( $editForm->get('apellido')->getErrors());
                print_r( $editForm->get('nombre')->getErrors());
                print_r( $editForm->get('apellido')->getErrors());
                print_r( $editForm->get('email')->getErrors());
                print_r( $editForm->get('codigoPostal')->getErrors());
                print_r( $editForm->get('direccion')->getErrors());
                print_r( $editForm->get('telefono')->getErrors());
                
             
                 $this->createMsgEdit($return, false, $entity,array($editForm->getErrors()));
            }
        }
        return $this->createAjaxResponse($return);
    }

    private function createMsgEdit(&$return, $okStatus = false, $entity, $errors=array()) {
        if ($okStatus) {
            $return['status'] = true;
            $return['mensaje'] = "Editado con exito";
            $return['entity'] = $entity->getJsonForAjaxEdit();
           
        } else {
            $return['status'] = false;
            $return['mensaje'] = "Ocurrio un error al intentar editar";
            $return['entity'] = $entity->getJsonForAjaxEdit();
             $return['errors'] =$errors;
        }
        return $return;
    }

    private function createAjaxResponse($return) {
        return new Response(json_encode($return), 200, array('Content-Type' => 'application/json'));
    }

}

?>
